package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.admin.service.ISysConfigService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.SysConfigMapper;
import com.ee.chat.common.entity.SysConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Shane
 * @Date 2020/3/15 22:56
 */
@Service
public class SysConfigService implements ISysConfigService {
    @Resource
    SysConfigMapper sysConfigMapper;
    @Resource
    IOperationService operationService;

    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        Page<SysConfig> p = new Page<>(page, limit);
        IPage<SysConfig> iPage = sysConfigMapper.list(p);
        List<SysConfig> datas = iPage.getRecords();

        data.put("datas", datas);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取系统配置数据列表");

        return Result.returnSuccess(data);
    }

    @Override
    public Result edit(HttpServletRequest request, Integer id, String value) throws Exception {
        SysConfig sysConfig = sysConfigMapper.selectById(id);

        sysConfig.setValue(value);
        sysConfigMapper.updateById(sysConfig);

        return Result.returnSuccess(null);
    }
}
